#include<iostream>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include<sys/wait.h>

using namespace std;

int main()
{
    int fd[2];
    int check = pipe(fd);
    char test_buff[64] = "this is a communication test";
    if (check != 0)
    {
        std::cout << "create pipe error" <<endl;
        return 0;
    }

    pid_t id = fork();
    if (id > 0)
    {
        close(fd[0]);
        write(fd[1], test_buff, sizeof(test_buff));
        std::cout << "我是父进程，我的pid是"<<getpid() << endl;
    }

    if (id == 0)
    {
        close(fd[1]);
        while (true)
        {
            sleep(1);
            memset(test_buff, 0, sizeof(test_buff));
            read(fd[0], test_buff, sizeof(test_buff)-1);
            std::cout << "我是子进程，我的pid是"<<getpid() << endl;
            std::cout << test_buff << endl;
        }
    }
    
    wait(nullptr);
    return 0;
}